<?php

namespace App\Recharge\Enums;

use App\Models\InterfaceConfiguration;
use App\Recharge\Library;

enum ApiGroupEnum
{

    /**
     * api直冲
     */
    case DirectCharge;

    /**
     * 循环充值
     */
    case CyclicImpulse;

    /**
     * 组合充值
     */
    case CombinationRecharge;

    /**
     * 商户组合充值
     */
    case MerchantCombinationRecharge;

    /**
     * @param string $order_sn
     * @return string
     */
    public function order_sn(string $order_sn): string
    {
        return match ($this) {
            self::CyclicImpulse => 'XH' . $order_sn ,
            self::CombinationRecharge => 'ZH' . $order_sn,
            default => $order_sn,
        };
    }

    /**
     * @param string $order_sn
     * @return ApiGroupEnum
     */
    public static function identifyByOrderSn(string $order_sn): ApiGroupEnum
    {
        return match (true) {
            str_contains($order_sn, 'XH') => self::CyclicImpulse,
            str_contains($order_sn, 'ZH') => self::CombinationRecharge,
            str_contains($order_sn, 'MCB') => self::MerchantCombinationRecharge,
            default => self::DirectCharge,
        };
    }

    /**
     * @param InterfaceConfiguration|int $api
     * @return ApiGroupEnum
     */
    static public function identify(InterfaceConfiguration|int $api): ApiGroupEnum
    {
        return match ($api instanceof InterfaceConfiguration ? $api->id : $api) {
            2,30        => self::CyclicImpulse,
            3,31        => self::CombinationRecharge,
            default     => self::DirectCharge,
        };
    }

}
